package cn.big.bigd.biocode.service;

import cn.big.bigd.biocode.dto.Result;
import cn.big.bigd.biocode.model.Publication;
import cn.big.bigd.biocode.util.DateUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;

/**
 * Created by wangfan on 2017/12/4.
 */
@Service
public class PmidService {

    public static Result getPublicationByPmidJson(String pmid) {
        try {
            RestTemplate template = new RestTemplate();
            Map p = template.postForObject("https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?id=" + pmid + "&db=pubmed&retmode=json", null, Map.class);
            Map result = (Map) p.get("result");
            if (p.containsKey("error") || !result.containsKey(pmid)) {
                return Result.build(1, "pmid error", p);
            }
            Map pmidMap = (Map) result.get(pmid);
            if (!pmidMap.containsKey("articleids")) {
                return Result.build(1, "pmid error", p);
            }
            Publication publication = new Publication();
            publication.setPmid(pmid);
            publication.setJournal(pmidMap.get("fulljournalname").toString());
            publication.setDate(DateUtils.format(pmidMap.get("pubdate").toString()));
            publication.setIssue(pmidMap.get("issue").toString());
            publication.setVolume(pmidMap.get("volume").toString());
            publication.setTitle(pmidMap.get("title").toString());
            publication.setDoi("");
            List<Map> articleids = (List<Map>) pmidMap.get("articleids");
            for (Map articleid : articleids) {
                if ("doi".equals(articleid.get("idtype"))) {
                    publication.setDoi(articleid.get("value").toString());
                }
            }
            List<Map> authors = (List<Map>) pmidMap.get("authors");
            String[] arr = new String[authors.size()];
            for (int i = 0; i < authors.size(); i++) {
                Map author = authors.get(i);
                arr[i] = author.get("name").toString();

            }
            publication.setAuthors(StringUtils.join(arr, ","));
            return Result.ok(publication);
        } catch (Exception e) {
            return Result.build(1, "has error", "");
        }

    }

    public static Result getPublicationByDoiJson(String doi) {
        RestTemplate template = new RestTemplate();
        Map p = template.getForObject("https://api.crossref.org/v1/works/" + doi, Map.class);
        if (!p.containsKey("message")) {
            return Result.build(1, "pmid error", p);
        }
        Map message = (Map) p.get("message");
        Publication publication = new Publication();
        publication.setPmid("");
        publication.setJournal(((List<String>) message.get("container-title")).get(0).toString());
        publication.setIssue(message.get("issue").toString());
        publication.setVolume(message.get("volume").toString());
        List<String> titles = (List<String>) message.get("title");
        publication.setTitle(titles.get(0).toString());
        publication.setDoi(doi);

        List<Map> authors = (List<Map>) message.get("author");
        String[] arr = new String[authors.size()];
        for (int i = 0; i < authors.size(); i++) {
            Map author = authors.get(i);
            arr[i] = author.get("given").toString() + " " + author.get("family").toString();

        }
        publication.setAuthors(StringUtils.join(arr, ","));


        Map created = (Map) message.get("created");
        List<Integer> dateParts = (List<Integer>) ((List<Map>) created.get("date-parts")).get(0);
        publication.setDate(org.springframework.util.StringUtils.collectionToDelimitedString(dateParts, "-"));
        return Result.ok(publication);

    }


  /*  public static void main(String[] args) {
        System.out.println(PmidService.getPublicationByPmidJson("23259851").toString());;
        System.out.println(PmidService.getPublicationByDoiJson("10.1186/1471-2105-13-335").toString());;
    }*/
}
